/*window.onbeforeunload = function() {
	Ext.Ajax.request({
		url :'rest/login/closeWeb',
	});
};*/
var currentORG = null;
var currentSpace = null;
var currentUserName = null;
var currentUserId = null;
var currentUserIsAdmin = 0;
Ext.define('cfWeb.controller.util.NavController', {
    extend: 'Ext.app.Controller',
    views: ['cfWeb.cfWebComponent.NavView'
        , 'accountSetting.AccountSettingWindow'
        , 'cfWeb.view.accountSetting.SettingWindow'
        , 'cfWeb.cfWebComponent.CfPanelTypeII'
        , 'cfWeb.cfWebComponent.PanelHeaderTypeII'
        , 'cfWeb.view.messageManage.MessagesWindows'
        , 'cfWeb.view.messageManage.Messages'
        , 'cfWeb.view.accountSetting.AuthOrgUserWindow'
        , 'cfWeb.cfWebComponent.VariableListView'
        , 'accountSetting.ChangePasswdWindow'
    ],


    stores: ['message.MessageStore'],

    init: function() {
        this.control({
            'navView': {
                onNavChange: this.navChange,
                viewready: this.setNav,
                onMenuClicked: this.onMenuClicked,
                afterrender: this.getData
            }
        });
    },
    getData: function(me) {
        Ext.Ajax.request({
            url: 'rest/login/currentCloud',
            method: 'GET',
            success: function(resp) {
                var data = Ext.decode(resp.responseText).data;
                me.platform = data; //测试cf
            },
            failure: function(resp) {
                console.log('failure');
            }
        });

    },
    setNav: function(me) {
        me.setMessageCount(0);
        Ext.Ajax.request({
            url: 'rest/login/getCurrentUser',
            success: function(re) {
                var response = Ext.JSON.decode(re.responseText);
                console.log(response);
                console.log(response.username);
                me.setUser(response.username, 'resources/img/avatar.jpeg');
            },
            failure: function(re) {
                console.log(cfWebLang.Util.UnLogginUser);
                //避免覆盖会话超时框
                //Ext.Msg.alert(cfWebLang.Util.Tip, cfWebLang.Util.UnLogginUser);
            }
        });

        var urlString = window.location.href;
        var index = urlString.indexOf('#');
        var urlStringNew = urlString.substring(index + 1);
        if (urlStringNew == window.location.href) {
            urlStringNew = 'homeTabView';
        }
        ;
        switch (urlStringNew) {
            case 'homeTabView': me.setNav('index');
                break;
            case 'templateManageView':me.setNav('templateManage');
                break;
            case 'appMonitorView':me.setNav('appMonitor');
                break;
            case 'appManageView': me.setNav('appManage');
                break;
            case 'dataStorageView': me.setNav('dataStorageService');
                break;
            case 'dataAnalyseView': me.setNav('dataAnalyseService');
                break;
            case 'appServiceView': me.setNav('appService');
                break;
            case 'appView': me.setNav('app');
                break;
            case 'appAuditView': me.setNav('appAudit');
                break;
        }
    },
    topBarInit: function(view, opts) {
        var list = Ext.select('div[class=\'\'nav_list] li');
        list.on('click', this.navChange, this, list);
        console.log(list);
    },
    navChange: function(el, target, NavView) {
        console.log('navChange to', target);
        NavView.setMessageCount('0');
        var urlString = window.location.href;
        var index = urlString.indexOf('#');
        var urlStringNew = urlString.substring(0, index);
        if (urlStringNew.length == 0) {
            urlStringNew = urlString;
        }
        if (target == 'index') {
            updateDash(); // update dash data
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('homeTabView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#homeTabView';
        } else if (target == 'appManage') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('appManageView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#appManageView';
        } else if (target == 'dataStorageService') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('dataStorageView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#dataStorageView';
        } else if (target == 'dataAnalyseService') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('dataAnalyseView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#dataAnalyseView';
        } else if (target == 'appService') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('appServiceView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#appServiceView';
        } else if (target == 'app') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('appView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#appView';
        } else if (target == 'templateManage') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('templateManageView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#templateManageView';
        } else if (target == 'appMonitor') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('appMonitorView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#appMonitorView';
        } else if(target == 'appAudit'){
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var newView = Ext.widget('appAuditView');
            mainView.changeMainView(newView);
            window.location.href = urlStringNew + '#appAuditView';
        }
    },

    languageChange: function(lang) {
        var language = getCookie('language');
        if (language == 'cn') {
            //选择英文字体
            setCookie('language', 'en', 365);
            var language = getCookie('language');
            //			alert(language);
            window.location.reload();
        } else {
            //选择中文字体
            setCookie('language', 'cn', 365);
            var language = getCookie('language');
            //			alert(language);
            window.location.reload();
        }
    },

    onMenuClicked: function(dataId) {
        console.log(dataId);
        if (dataId == 'setting') {
            //			var window = Ext.widget('accountSettingWindow');
            //			window.show();
            var window = Ext.widget('SettingWindow', {
                dockedItems: [{
                    xtype: 'container',
                    dock: 'bottom',
                    height: 20,
                    width: 100,
                    style: {
                        'background-color': '#fff'
                    }
                }

                ]
            });
            Ext.Ajax.request({
                url: 'rest/user/currentuserOrgSpace',
                method: 'GET',
                root: 'data',
                success: function(resp) {
                    console.log(resp);
                    console.log('success');
                    var select = Ext.JSON.decode(resp.responseText);
                    var data = select.data;
                    window.currentOrg = data[0];
                    window.currentSpace = data[1];
                },
                failure: function(resp) {
                    console.log(resp);
                    console.log('failure');
                }
            });
            console.log(window);
            window.show();
        }
        if (dataId == 'message') {
            var mainView = Ext.ComponentQuery.query('mainView')[0];
            var slider = Ext.widget('messageSlideView', {
                width: 300,
                adaptTo: mainView,
                verticalIn: false,
                closeSign: false
            });
            slider.slideIn();

        //			var window = Ext.widget('messagesWindows');
        //			window.show();
        }
        if (dataId == 'accountSetting') {
            var window = Ext.widget('changePasswdWindow');
            window.show();
        }
        if (dataId == 'exit') {
            console.log('helloexit');
            var username = '';
            var lastOrg = '';
            var lastSpace = '';

            Ext.Ajax.request({
                url: 'rest/login/getCurrentUser',
                success: function(resp) {
                    var response = Ext.JSON.decode(resp.responseText);
                    console.log(response);
                    console.log('1!!!!!!!!!!!!');
                    username = response.username;
                    lastOrg = response.org;
                    lastSpace = response.space;

                    console.log(response.username);
                    console.log(username);
                    console.log(lastOrg);
                    console.log(lastSpace);
                },
                failure: function(reps) {}
            });
            Ext.Ajax.request({
                url: 'rest/login/logout',
                method: 'GET',
                params: {
                    username: username,
                    lastOrg: lastOrg,
                    lastSpace: lastSpace
                },
                success: function(resp) {
                    console.log(resp);
                    parent.location.href = '/cfWeb/login.html';
                },
                failure: function() {}
            })
        }

    }
});
